package com.mapforge;

import java.awt.Dimension;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.mapforge.exceptions.MissingResourceException;
import com.mapforge.map.TileMap;
import com.mapforge.util.FileUtil;

/**
 * The {@link Constants} class gathers frequently referenced resources used in
 * this editor.
 * 
 * @author Will Morrison
 * 
 */
public final class Constants {
	/**
	 * Private constructor, no instance of {@link Constants} can be constructed
	 * since there's no reason for thier to be an instance.
	 */
	private Constants() {
	}

	/**
	 * The application title
	 */
	public static final String APP_TITLE = "MapForge";

	/**
	 * Map editor application version
	 */
	public static String APP_VERSION = "0.3.0";

	/** Maximum number of tiles allowed in a tileset. */
	public static final int MAX_TILESET_TILES = 1000;

	/**
	 * Starting Tileset dimension.
	 */
	public static final Dimension DEFAULT_TILE_DIMENSION = new Dimension(32, 32);
	/**
	 * Maximum dimension for a single tile.
	 */
	public static final Dimension MAX_TILE_DIMENSION = new Dimension(1024, 1024);
	/**
	 * Maximum number of tiles allowed in a {@link TileMap}
	 */
	public static final Dimension MAX_MAP_DIMENSION = new Dimension(500, 500);

	public static final String ICON_DIR = "Data" + File.separatorChar + "Icons"
			+ File.separatorChar;
	/**
	 * Whether this product is under development.
	 */
	public static final boolean DISTRIBUTE = false;

	/**
	 * Mapping for icon file names (minus extensions) to their image
	 * representation.
	 */
	private static final HashMap<String, Image> ICONS_HASHMAP = new HashMap<String, Image>();

	// edits having no button tool representation
	public static final ImageIcon LAYER_EDITED = new ImageIcon();
	public static final ImageIcon TILESET_EDIT = new ImageIcon();
	public static final ImageIcon COMPOUND_EDIT = new ImageIcon();
	public static final ImageIcon CUTTILES_EDIT = new ImageIcon();
	public static final ImageIcon RENAME_EDIT = new ImageIcon();

	// Tool icons (also represent their respective edits)
	public static final ImageIcon ERASER_ICON = new ImageIcon();
	public static final ImageIcon PENCIL_ICON = new ImageIcon();
	public static final ImageIcon SPRAYPAINT_ICON = new ImageIcon();
	public static final ImageIcon LINE_ICON = new ImageIcon();
	public static final ImageIcon ZOOM_IN_ICON = new ImageIcon();
	public static final ImageIcon ZOOM_OUT_ICON = new ImageIcon();
	public static final ImageIcon FLOODFILL_ICON = new ImageIcon();
	public static final ImageIcon RECTANGLE_ICON = new ImageIcon();
	public static final ImageIcon ELLIPSE_ICON = new ImageIcon();

	// Selection type icons
	public static final ImageIcon FLOODFILL_SELECTION_ICON = new ImageIcon();
	public static final ImageIcon RECTANGLE_SELECTION_ICON = new ImageIcon();
	public static final ImageIcon ELLIPSE_SELECTION_ICON = new ImageIcon();
	public static final ImageIcon SINGLE_ROW_SELECTION_ICON = new ImageIcon();
	public static final ImageIcon SINGLE_COLUMN_SELECTION_ICON = new ImageIcon();

	// Selection behavior icons
	public static final ImageIcon ADD_SELECTION = new ImageIcon();
	public static final ImageIcon SUBTRACT_SELECTION = new ImageIcon();
	public static final ImageIcon INTERSECT_SELECTION = new ImageIcon();
	public static final ImageIcon NEW_SELECTION = new ImageIcon();

	// Tile writer icons
	public static final ImageIcon TILEWRITE_SEQUENTIAL = new ImageIcon();
	public static final ImageIcon TILEWRITE_RANDOM = new ImageIcon();

	// tile layer icons
	public static final ImageIcon EYE_OPEN_ICON = new ImageIcon();
	public static final ImageIcon EYE_CLOSED_ICON = new ImageIcon();
	public static final ImageIcon PADLOCK_LOCKED_ICON = new ImageIcon();
	public static final ImageIcon PADLOCK_UNLOCKED_ICON = new ImageIcon();

	public static final ImageIcon SPLASHSCREEN_ICON = new ImageIcon();
	public static final ImageIcon WINDOW_FOCUSED_ICON = new ImageIcon();
	public static final ImageIcon WINDOW_UNFOCUSED_ICON = new ImageIcon();

	public static final void load() {
		// load all files in icon directory, map by name
		try {
			File file = new File(ICON_DIR);
			if (file.exists()) {
				String[] fileNames = file.list();
				for (int i = 0; i < fileNames.length; i++) {
					File imageFile = new File(ICON_DIR + fileNames[i]);
					if (FileUtil.getExt(imageFile).equals("png")) {
						ICONS_HASHMAP.put(FileUtil
								.getFileNameWithoutExt(imageFile), ImageIO
								.read(imageFile));
					}
				}
			}
			LAYER_EDITED.setImage(getImage("layeredit"));
			TILESET_EDIT.setImage(getImage("tileset_edit"));
			COMPOUND_EDIT.setImage(getImage("compound_edit"));
			CUTTILES_EDIT.setImage(getImage("cut_tiles"));
			RENAME_EDIT.setImage(getImage("rename"));

			// Tool icons (also represent their respective edits)
			ERASER_ICON.setImage(getImage("eraser"));
			PENCIL_ICON.setImage(getImage("pencil"));
			SPRAYPAINT_ICON.setImage(getImage("spraypaint"));
			LINE_ICON.setImage(getImage("line"));
			ZOOM_IN_ICON.setImage(getImage("zoom_in_icon"));
			ZOOM_OUT_ICON.setImage(getImage("zoom_out_icon"));
			FLOODFILL_ICON.setImage(getImage("floodfill"));
			RECTANGLE_ICON.setImage(getImage("rectangle"));
			ELLIPSE_ICON.setImage(getImage("ellipse"));

			// Selection type icons
			FLOODFILL_SELECTION_ICON.setImage(getImage("quick_selection"));
			RECTANGLE_SELECTION_ICON.setImage(getImage("rectangle_selection"));
			ELLIPSE_SELECTION_ICON.setImage(getImage("ellipse_selection"));
			SINGLE_ROW_SELECTION_ICON.setImage(getImage("row_selection_icon"));
			SINGLE_COLUMN_SELECTION_ICON
					.setImage(getImage("column_selection_icon"));

			// Selection behavior icons
			ADD_SELECTION.setImage(getImage("addto_selection"));
			SUBTRACT_SELECTION.setImage(getImage("subtractfrom_selection"));
			INTERSECT_SELECTION.setImage(getImage("intersect_selection"));
			NEW_SELECTION.setImage(getImage("new_selection"));

			// Tile writer icons
			TILEWRITE_SEQUENTIAL.setImage(getImage("tilewrite_sequential"));
			TILEWRITE_RANDOM.setImage(getImage("tilewrite_random"));

			// tile layer icons
			EYE_OPEN_ICON.setImage(getImage("eye_open_icon"));
			EYE_CLOSED_ICON.setImage(getImage("eye_closed_icon"));
			PADLOCK_LOCKED_ICON.setImage(getImage("padlock_locked_icon"));
			PADLOCK_UNLOCKED_ICON.setImage(getImage("padlock_unlocked_icon"));

			SPLASHSCREEN_ICON.setImage(getImage("splash_screen"));
			WINDOW_FOCUSED_ICON.setImage(getImage("closeWindowFocused"));
			WINDOW_UNFOCUSED_ICON.setImage(getImage("closeWindowUnfocused"));
		} catch (IOException e) {
			System.err
					.println("IOException reading image file within icon directory \""
							+ ICON_DIR + "\"");
			e.printStackTrace();
		}
	}

	public static Image getImage(String name) {
		if (ICONS_HASHMAP.containsKey(name)) {
			return ICONS_HASHMAP.get(name);
		} else {
			throw new MissingResourceException(name + " icon");
		}
	}
}
